package com.qf.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.*;
import com.qf.vo.CatVo;
import com.qf.vo.PermUpdateVo;
import com.qf.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @author lixu
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/role")
public class RoleController {


    @Autowired
    private DtsRoleService dtsRoleService;
    @Autowired  // 上下文  应用域 application
    private ApplicationContext applicationContext;
    @Autowired
    private DtsPermissionService dtsPermissionService;
    /**
     * - 接口地址 : http://localhost:8083/admin/role/options
     * - GET请求
     */
    @RequiresAuthentication
    @GetMapping("/options")
    public Object options(){
        List<CatVo> dtsRoleList = dtsRoleService.findRoleList();
        return ResponseUtil.ok(dtsRoleList);
    }
    /**
     * http://localhost:8080/admin/role/list?page=1&limit=20&sort=add_time&order=desc&rolename=111
     * 角色管理之 列表查询 分页查询
     */
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "查询")
    @RequiresPermissions(value = "admin:role:list")
    @GetMapping(value = {"/list"})
    public Object list(Integer page,Integer limit,String sort,String order,String rolename){

        IPage<DtsRole> ipage = dtsRoleService.list(page,limit,sort,order,rolename);

        Map<String,Object> data = new HashMap<>();
        data.put("items",ipage.getRecords());
        data.put("total",ipage.getTotal());

        return ResponseUtil.ok(data);
    }

    /**
     * 授权列表
     *   1、显示当前系统中已经完成的功能模块  的授权列表
     *
     *   2、将当前角色 选中对应的授权
     *
     *   - 接口地址 : http://localhost:8083/admin/role/permissions?roleId=5
     *
     * - 下行数据 :
     */
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "授权列表")
    @RequiresPermissions(value = "admin:role:perm:list")
    @GetMapping("/permissions")
    public Object permissionsList(Integer roleId){

        //1、显示当前系统中已经完成的功能模块  的授权列表
        List<PermVo> permVoList =  findPermVoList();

        //2、将当前角色 所有权限列表查询出来
        Set<String> assignedPermissions = dtsPermissionService.findPermissionListByRoleId(roleId);

        Map<String,Object> data = new HashMap<>();
        //显示1
        data.put("systemPermissions",permVoList);
        //显示2
        data.put("assignedPermissions",assignedPermissions);
        return ResponseUtil.ok(data);
    }

    /**
     *  授权列表 上 确定
     * @return
     */
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "授权操作")
    @RequiresPermissions(value = "admin:role:perm:update")
    @PostMapping("/permissions")
    public Object permissionsConfirm(@RequestBody PermUpdateVo permUpdateVo){

        dtsPermissionService.update(permUpdateVo);

        return ResponseUtil.ok();
    }



    //显示当前系统中已经完成的功能模块  的授权列表
    private List<PermVo> findPermVoList() {
        List<Permission> permissionList = PermissionUtil.
                listPermission(applicationContext, "com.qf.controller");
        //偷天换日
        if(!CollectionUtils.isEmpty(permissionList)){
            return PermissionUtil.listPermVo(permissionList);
        }else{
            //空集合  不是NULL
            return new ArrayList<PermVo>();
        }
    }

    /**
     * 查询角色详情
     * @param id
     * @return
     */
    @RequiresPermissions("admin:role:read")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色详情")
    @GetMapping("/read")
    public Object read(Integer id) {

        DtsRole role = dtsRoleService.findById(id);

        return ResponseUtil.ok(role);
    }


    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole role) {


        if (dtsRoleService.checkExist(role.getName())) {
            return AdminResponseUtil.fail(AdminResponseCode.ROLE_NAME_EXIST);
        }

        dtsRoleService.add(role);

        return ResponseUtil.ok(role);
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole role) {

        dtsRoleService.updateById(role);
        return ResponseUtil.ok();
    }

    /**
     * 删除角色
     * @param role
     * @return
     */
    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole role) {

        Integer id = role.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        dtsRoleService.deleteById(id);

        return ResponseUtil.ok();
    }
}
